package com.ruoyi.web.controller.ring.app;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ring.domain.RingInteractiveComments;
import com.ruoyi.ring.domain.RingInteractiveThumbs;
import com.ruoyi.ring.domain.dto.InteractiveCommentsDTO;
import com.ruoyi.ring.domain.dto.InteractiveThumbsDTO;
import com.ruoyi.ring.domain.dto.QueryShareDTO;
import com.ruoyi.ring.domain.dto.ShareSignAddDTO;
import com.ruoyi.ring.service.IRingInteractiveCommentsService;
import com.ruoyi.ring.service.IRingInteractiveSharingService;
import com.ruoyi.ring.service.IRingInteractiveThumbsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 互动分享 app
 * 
 * @author liuwp
 * @date 2025-11-21
 */
@Slf4j
@RestController
@RequestMapping("/ring/app_interactive_sharing")
public class AppInteractiveSharingController extends BaseController
{
    @Resource
    private IRingInteractiveSharingService ringInteractiveSharingService;
    @Resource
    private IRingInteractiveCommentsService ringInteractiveCommentsService;
    @Resource
    private IRingInteractiveThumbsService ringInteractiveThumbsService;



    /**
     * 自动分享今日签分享 app(最后加载此接口)
     */
    @PostMapping("/add-automatic-share-sign")
    public void addAutomaticShareSign(String deviceSn){
        ringInteractiveSharingService.addAutomaticShareSign(deviceSn);
    }
    /**
     * 手动分享今日签分享 app
     */
    @PostMapping("/add-manual-share-sign")
    public AjaxResult addManualShareSign(@RequestBody ShareSignAddDTO dto){
        return toAjax(ringInteractiveSharingService.addManualShareSign(dto));
    }

    /**
     * 互动分享列表  app
     */
    @GetMapping("/query-share-list")
    public AjaxResult queryShareList(QueryShareDTO shareDTO){
        long userId = SecurityUtils.getUserId();
        log.info("互动分享列表queryShareList: userId={}, param={}", userId, JSON.toJSONString(shareDTO));
        return AjaxResult.success(ringInteractiveSharingService.queryShareList(shareDTO));
    }
    /**
     * 删除互动分享 app
     */
    @DeleteMapping("/share-delete/{shareId}")
    public AjaxResult deleteShare(@PathVariable Long shareId){
        ringInteractiveCommentsService.deleteRingInteractiveCommentsByCondition(Builder.of(RingInteractiveComments::new).with(RingInteractiveComments::setShareId,shareId).build());
        ringInteractiveThumbsService.deleteRingInteractiveThumbsByCondition(Builder.of(RingInteractiveThumbs::new).with(RingInteractiveThumbs::setShareId,shareId).build());
        return toAjax(ringInteractiveSharingService.deleteRingInteractiveSharingById(shareId));
    }

    /**
     * 评论点赞 app
     */
    @PostMapping("/share-comment-thumb")
    public AjaxResult commentThumb(@RequestBody InteractiveThumbsDTO dto){
        RingInteractiveThumbs ringInteractiveThumbs = Convert.convert(RingInteractiveThumbs.class,dto);
        return toAjax(ringInteractiveThumbsService.insertRingInteractiveThumbs(ringInteractiveThumbs));
    }
    /**
     * 取消点赞 app
     */
    @DeleteMapping("/share-delete-thumb/{thumbId}")
    public AjaxResult deleteThumb(@PathVariable Long thumbId){
        return toAjax(ringInteractiveThumbsService.deleteRingInteractiveThumbsById(thumbId));
    }

    /**
     * 创建评论 app
     */
    @PostMapping("/share-create-comment")
    public AjaxResult createComment(@RequestBody InteractiveCommentsDTO dto){
        RingInteractiveComments ringInteractiveComments = Convert.convert(RingInteractiveComments.class,dto);
        return toAjax(ringInteractiveCommentsService.insertRingInteractiveComments(ringInteractiveComments));
    }
    /**
     * 删除评论 app
     */
    @DeleteMapping("/share-delete-comment/{commentId}")
    public AjaxResult deleteComment(@PathVariable Long commentId){
        return toAjax(ringInteractiveCommentsService.deleteRingInteractiveCommentsById(commentId));
    }

}
